<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends MY_Controller {

	function __construct()
    {
		// Call the Model constructor
        parent::__construct();
		if($this->session->userdata('login') === TRUE && in_array(146,$this->access)){
			$log['activity']='VIEW';
			$log['information']='Masuk ke menu : '.$this->uri->uri_string();
			$this->model_log->add($log);
		}else{
			$log['activity']='VIEW';
			$log['information']='Tidak diijinkan : '.$this->uri->uri_string();
			$this->model_log->add($log);
			redirect('sessions/login');
		}
		
		$this->config->set_item("menu_active","account");
    }
	
	public function index($action="")
	{
		$this->load->model('model_user');
		
		$id = $this->session->userdata("user_id");
		
		if(isset($_POST['data'])){
			if($action == "account"){
				$respon = array();
				$error = false;
				$count = 0;
				
				if(!$this->model_user->check_username($_POST['data']['username'],$id)){
					$respon['step'] = 1;
					$respon['msg'][$count]['id'] = "username";
					$respon['msg'][$count]['text'] = "Username sudah terdaftar dalam sistem";
					
					$count++;
					$error = true;
				}
				
				if($error){
					$respon['status'] = "error";
				}else{
					
					$user = $_POST['data'];
					
					$this->model_user->edit($id,$user);
					
					$respon['status'] = "success";
					$respon['msg'] = "Data berhasil disimpan";
				}
				
				echo json_encode($respon);
				return;
			}else if($action == "change-password"){
				$respon = array();	
				$user = $_POST['data'];
				$user['password'] = sha1($user['password']);
				$this->model_user->edit($id,$user);
				
				$respon['status'] = "success";
				$respon['msg'] = "Password telah berubah";
				
				echo json_encode($respon);
				return;
			}
		}
		$data['dt'] = $this->model_user->get_user_by($id);

		$params['content']= $this->load->view('form',$data,TRUE);
		$params['footer']['js_plugin'] = $this->load->view('js_plugin','',TRUE);
		$params['header']['css_plugin'] = $this->load->view('css_plugin','',TRUE);
		
		echo modules::run('template', $params);	
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */